package com.mediacloud.app.newsmodule.fragment.ws

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eventTrack.CDPTrackReportManager
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.component.IEvent
import com.mediacloud.app.newsmodule.fragment.video.x.IVideoHolder
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.tencent.mmkv.MMKV

class WS24VideoListAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),
    View.OnClickListener {
    var items: MutableList<Any> = ArrayList()
    val color1 =Color.parseColor("#666666")
    val color2 =Color.parseColor("#222222")

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = when(viewType){
            0-> TitleImgHolder(LayoutInflater.from(context).inflate(R.layout.item_ws24_2, parent, false))
            2-> VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_ws24_big_video,parent,false))
            3-> SmallVideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_ws24_small_video,parent,false))
            else -> TitleHolder(LayoutInflater.from(context).inflate(R.layout.item_ws24_title, parent, false))
        }
        return holder
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val item = items[position]

        when (getItemViewType(position)) {
            0->{
                item as TimeLineItem
                val holder = viewHolder as TitleImgHolder
                holder.title.text = item.title
                holder.logo.loadX(item.logo, 1,1,false)
                if (item.isGroupFirst) {
                    holder.v0.visibility = View.INVISIBLE
                } else {
                    holder.v0.visibility = View.VISIBLE
                }
                holder.time.text = item.timeTitleTx
                val name =  if (TextUtils.isEmpty(item.referName)) context.resources.getString(R.string.app_name) else item.referName
                holder.timeInner.text = "$name"

                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    holder.title.setTextColor(color1)
                } else {
                    holder.title.setTextColor(color2)
                }
                viewHolder.itemView.tag = position
                viewHolder.itemView.setOnClickListener(this)
            }
            1->{
                item as WSDateItem
                val holder = viewHolder as TitleHolder
                if (holder.absoluteAdapterPosition == 0) {
                    holder.dateTv.visibility = View.GONE
                } else {
                    holder.dateTv.visibility = View.VISIBLE
                }
                holder. dateTv.text = item.title
            }
            2->{
                item as TimeLineItem
                val holder = viewHolder as VideoViewHolder
                holder.tvNewsTipV2.text = item.prop4
                holder.title.text = item.title
                holder.logo.loadX(item.logo, 3,1,false)
                if (item.isGroupFirst) {
                    holder.v0.visibility = View.INVISIBLE
                } else {
                    holder. v0.visibility = View.VISIBLE
                }
                holder. time.text = item.timeTitleTx
                val name =  if (TextUtils.isEmpty(item.referName)) context.resources.getString(R.string.app_name) else item.referName
                holder.timeInner.text = "$name"

                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    holder.title.setTextColor(color1)
                } else {
                    holder.title.setTextColor(color2)
                }

                val _item = ArticleItem()
                _item.id = item.id
                _item.title = item.title
                _item.type = item.type
                _item.logo = item.logo
                _item.positionInList = position
                _item.video = item.video
                holder.articleItem = _item

                viewHolder.itemView.tag = position
                viewHolder.itemView.setOnClickListener(this)
            }

            3->{
                item as TimeLineItem
                val holder = viewHolder as SmallVideoViewHolder
                holder.title.text = item.title
                holder.logo.loadX(item.logo, 1,1,false)
                if (item.isGroupFirst) {
                    holder.v0.visibility = View.INVISIBLE
                } else {
                    holder. v0.visibility = View.VISIBLE
                }
                holder. time.text = item.timeTitleTx
                val name =  if (TextUtils.isEmpty(item.referName)) context.resources.getString(R.string.app_name) else item.referName
                holder.timeInner.text = "$name"


                if (MMKV.defaultMMKV().getBoolean(item.id.toString() , false)) {
                    holder.title.setTextColor(color1)
                } else {
                    holder.title.setTextColor(color2)
                }

//                val _item = ArticleItem()
//                _item.id = item.id
//                _item.title = item.title
//                _item.type = item.type
//                _item.logo = item.logo
//                _item.positionInList = position
//                _item.video = item.video
//                holder.articleItem = _item
//
//                events.add(holder)
                viewHolder.itemView.tag = position
                viewHolder.itemView.setOnClickListener(this)
            }

        }
    }

    override fun getItemCount(): Int {
        return items.size
    }


    override fun getItemViewType(position: Int): Int {
        val item = items[position]
        return if (item is TimeLineItem){
            item.itemType
        }else {
            item as WSDateItem
            item.itemType
        }
    }

    fun setDataItems(articleItems: MutableList<Any>) {
        this.items.clear()
        this.items.addAll(articleItems)
        notifyDataSetChanged()
    }

    fun addItems(articleItems: MutableList<Any>) {
        var positionStart = this.items.size
        if (positionStart < 0) positionStart = 0
        this.items.addAll(articleItems)
        notifyItemRangeChanged(positionStart, this.items.size)

    }

    override fun onClick(v: View?) {
        val position = v?.tag as Int
        val item = ArticleItem()
        val _item = items[position] as TimeLineItem
        item.id = _item.id
        item.title = _item.title
        item.type = _item.type
        item.logo = _item.logo
        item.positionInList = position
        item.Author = _item.author
        item.referName=_item.referName
        item.publishdate = _item.publishdate_format
        item.authorNickName = _item.author
        item.url = _item.url
        CDPTrackReportManager.getInstance().NewsRPClick(position,item)
        MMKV.defaultMMKV().putBoolean(item.getId().toString() + "", true)
        v.findViewById<TextView>(R.id.title)?.setTextColor(color1)
        NewsItemClickUtils.OpenItemNewsHandle(
            context,
            item.getType(),
            item, CatalogItem()
        )
    }


}

class SmallVideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

    //var articleItem:ArticleItem?=null
    val title =  itemView.findViewById<TextView>(R.id.smallTitle)
    val logo =  itemView.findViewById<ImageView>(R.id.logo)
    val logoContainer =  itemView.findViewById<FrameLayout>(R.id.logoContainer)
    val logoLayout =  itemView.findViewById<FrameLayout>(R.id.logoLayout)
    val time = itemView.findViewById<TextView>(R.id.time)
    val timeInner =  itemView.findViewById<TextView>(R.id.timeInner)
    val v0 =  itemView.findViewById<View>(R.id.v0)
    val playerIcon =  itemView.findViewById<View>(R.id.iv_play_tag)


//    override fun destroy() {
//
//    }
//
//    override fun getLogLayout(): View {
//        return logoLayout
//    }
//
//    override fun getContainer(): FrameLayout {
//        return logoContainer
//    }
//
//    override fun getArticle(): ArticleItem? {
//        return articleItem
//    }
//
//    override fun getPlayActionView(): View? {
//        return playerIcon
//    }

}


class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
    IVideoHolder {

    var articleItem:ArticleItem?=null
    val title =  itemView.findViewById<TextView>(R.id.bigTitle)
    val logo =  itemView.findViewById<ImageView>(R.id.logo)
    val logoContainer =  itemView.findViewById<FrameLayout>(R.id.logoContainer)
    val logoLayout =  itemView.findViewById<FrameLayout>(R.id.logoLayout)
    val time = itemView.findViewById<TextView>(R.id.time)
    val timeInner =  itemView.findViewById<TextView>(R.id.timeInner)
    val v0 =  itemView.findViewById<View>(R.id.v0)
    val playerIcon =  itemView.findViewById<View>(R.id.iv_play_tag)
    val tvNewsTipV2 =  itemView.findViewById<TextView>(R.id.tvNewsTipV2)


    override fun getLogLayout(): View {
        return logoLayout
    }

    override fun getContainer(): FrameLayout {
        return logoContainer
    }

    override fun getArticle(): ArticleItem? {
        return articleItem
    }

    override fun getPlayActionView(): View? {
        return playerIcon
    }

}

class TitleImgHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    val title = itemView.findViewById<TextView>(R.id.title)
    val logo = itemView.findViewById<ImageView>(R.id.logo)
    val time = itemView.findViewById<TextView>(R.id.time)
    val timeInner = itemView.findViewById<TextView>(R.id.timeInner)
    val v0 = itemView.findViewById<View>(R.id.v0)
    val iv_play_tag = itemView.findViewById<ImageView>(R.id.iv_play_tag)
    val tvNewsTipV2 = itemView.findViewById<TextView>(R.id.tvNewsTipV2)


}

class TitleHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    val dateTv =itemView.findViewById<TextView>(R.id.dateTv)

}